package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorRole;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.ArrayList;

public class UINpcHecheng extends UIAbstractWindow implements ILBHandler
{
	//
	private final int INDEX_HECHENG_JEWEL = 1;
	private final int INDEX_HECHENG_LABEL_FIRST = 2;
	private final int INDEX_HECHENG_BAG_FIRST = INDEX_HECHENG_LABEL_FIRST + ActorItemLBHandler.NUM_LABEL_DIS;
	//
	private final int GROUP_HECHENG_JEWEL = 1;
	private final int GROUP_HECHENG_LABEL = 2;
	private final int GROUP_HECHENG_BAG = 3;
	//
	private ActorItemLBHandler m_bag_handler;
	//
	private ActorItem m_item_jewel;//需强化的宝石
	private ActorItem m_item_jewel_need;//强化所需的宝石
	private ActorItem[] m_bag_items_copy;//背包中的宝石
	private int m_num_need;//
	private int m_num_goted;//
	private long m_money_cost;

	public UINpcHecheng(int id)
	{
		super(id);
		//
		setBounds(0, 0, UITools.W_BG, UITools.H_BG);
		setCentre();
	}

	public void createNotify()
	{
		ArrayList list = new ArrayList();
		ActorItem[] items = GameManager.getInstance().m_role.m_bag_goods;
		for (int i = 0; i < items.length; i++) {
			if (items[i] == null) {
				continue;
			}
			if (items[i].m_type != ActorItem.TYPE_GOODS_MATERIAL) {
				continue;
			}
			if (items[i].m_type_detail == ActorItem.TYPE_MATERIAL_BAOSHI) {
				list.addElement(items[i]);
			}
		}
		int size = list.size();
		m_bag_items_copy = new ActorItem[ActorRole.NUM_BAG_GOODS];
		for (int i = 0; i < size; i++) {
			m_bag_items_copy[i] = (ActorItem) list.elementAt(i);
		}
		//
		m_bag_handler = new ActorItemLBHandler(this);
		m_bag_handler.setLabelByLabelId(ActorItem.TYPE_GOODS_MATERIAL);
		//
		this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
	}
	
	public int countGroupId(int mIndex)
	{
		if (mIndex == INDEX_HECHENG_JEWEL) {
			return GROUP_HECHENG_JEWEL;
		}
		else if (mIndex < INDEX_HECHENG_BAG_FIRST) {
			return GROUP_HECHENG_LABEL;
		}
		else {
			return GROUP_HECHENG_BAG;
		}
	}

	public int countGroupIndex(int mGroupId, int mIndex)
	{
		switch (mGroupId) {
			case GROUP_HECHENG_JEWEL:
				return mIndex - INDEX_HECHENG_JEWEL;
			case GROUP_HECHENG_LABEL:
				return mIndex - INDEX_HECHENG_LABEL_FIRST;
			default:
				return mIndex - INDEX_HECHENG_BAG_FIRST;
		}
	}

	public void actionOnIndexChanged()
	{
		m_bag_handler.updateOnStateOrIndexChanged() ;
	}

	//////////////////////////
	//IBagHandler
	//////////////////////////
	public Object[] getBagDatas()
	{
		return m_bag_items_copy;
	}

	public int getUpIndex()
	{
		return INDEX_HECHENG_JEWEL;
	}

	public int getDownIndex()
	{
		return INDEX_HECHENG_JEWEL;
	}

	public int getLeftIndex()
	{
		return INDEX_HECHENG_JEWEL;
	}

	public int getRightIndex()
	{
		return INDEX_HECHENG_JEWEL;
	}

	public int getFirstIndex(int idComp)
	{
		switch (idComp) {
			case AbsLBHandler.TYPE_BAG:
				return INDEX_HECHENG_BAG_FIRST;
			case AbsLBHandler.TYPE_LABEL:
				return INDEX_HECHENG_LABEL_FIRST;
			default:
				throw new IllegalArgumentException("无此组Id = " + idComp);
		}
	}

	public int getGroupIndex()
	{
		return this.m_groupIndex;
	}

	public int getWindowIndex()
	{
		return this.m_index;
	}

	//////////////////////////
	//IBagHandler
	//////////////////////////
	private boolean m_isOperLocked = false;

	protected void keyLogic()
	{
		if (m_isOperLocked) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			//确定
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			//返回
			close();
		}
		else {
			switch (this.m_groupId) {
				case GROUP_HECHENG_JEWEL:
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
						this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
						this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
						this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
						this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						this.setWindowIndex(INDEX_HECHENG_BAG_FIRST);
					}
					break;
				case GROUP_HECHENG_BAG:
					int dir = m_bag_handler.keyDir(AbsLBHandler.TYPE_BAG);
					if (dir == AbsLBHandler.TYPE_DIR_NONE) {
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							clickBagItem();
						}
					}
					else if (dir == AbsLBHandler.TYPE_DIR_UNPRO) {
						//不处理
					}
					else {
						this.setWindowIndex(dir);
					}
					break;
			}
		}
	}

	private void clickBagItem()
	{
		int dataIndex = m_bag_handler.getCurrentBagDataIndex();
		if (m_bag_items_copy[dataIndex] == null) {
			return;
		}
		//卸下
		if (m_item_jewel != null) {
			m_bag_items_copy[m_item_jewel.m_index] = m_item_jewel;
		}
		//
		m_item_jewel = m_bag_items_copy[dataIndex];
		m_bag_items_copy[dataIndex] = null;
		m_isOperLocked = true;
		//发送请求合成宝石内容
		testSendData();
	}

	private void testSendData()
	{
		m_item_jewel_need = ActorItem.getTestItem(ActorItem.TYPE_BAG_ROLE, 2);
		m_num_need = 20;
		String key = m_item_jewel_need.m_key;
		for (int i = 0, length = m_bag_items_copy.length; i < length; i++) {
			if (m_bag_items_copy[i] == null) {
				continue;
			}
			if (key == m_bag_items_copy[i].m_key) {
				m_num_goted += m_bag_items_copy[i].m_num;;
			}
		}
		m_money_cost = 100;
		m_isOperLocked = false;
	}

	public void recvMessage(DataInputStream dis) throws IOException
	{
		recvHechengInfo(dis);
	}

	/**
	 * 获得宝石合成信息
	 * @param dis
	 * @throws IOException
	 */
	private void recvHechengInfo(DataInputStream dis) throws IOException
	{
		m_item_jewel_need = null;//获得当前所需的宝石对象
		m_num_need = dis.readInt();
		String key = m_item_jewel.m_key;
		for (int i = 0, length = m_bag_items_copy.length; i < length; i++) {
			if (m_bag_items_copy[i] == null) {
				continue;
			}
			if (key == m_bag_items_copy[i].m_key) {
				m_num_goted += m_bag_items_copy[i].m_num;;
			}
		}
		m_money_cost = dis.readLong();
		m_isOperLocked = false;
	}

	//
	private final String TITLE = "宝石合成";

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, TITLE) ;
		paintItemJewel(g, m_x + 20, m_y + 40);
		paintItemNeed(g, m_x + 20, m_y + 65);
		paintItemNum(g, m_x + 80, m_y + 80);
		paintNumNeed(g, m_x + 140, m_y + 80);
		UITools.paintMoneyGameLT(g, m_x + 20, m_y + 100, m_money_cost,false);
		int tempY = m_y + 120 ;
		m_bag_handler.setInitXY(tempY);
		m_bag_handler.paintLabel(g);
		m_bag_handler.paintBag(g);
		m_bag_handler.paintDrag(g);
		m_bag_handler.paintBagFocused(g);
		m_bag_handler.paintDescribe(g);
	}

	private void paintItemJewel(Graphics g, int x, int y)
	{
		UITools.paintActorItem(g, x, y, m_item_jewel, false, this.m_groupId == GROUP_HECHENG_JEWEL, true);
	}

	private void paintItemNeed(Graphics g, int x, int y)
	{
		UITools.paintActorItem(g, x, y, m_item_jewel_need);
	}

	private void paintNumNeed(Graphics g, int x, int y)
	{
		g.setColor(0x872232);
		g.drawString(String.valueOf(m_num_need), x - 1, y, Graphics.TOP | Graphics.RIGHT);
		g.setColor(0x8782979);
		g.drawString("/", x, y, Graphics.TOP | Graphics.LEFT);
		g.setColor(0x8d1212);
		g.drawString(String.valueOf(m_num_goted), x + 10, y, Graphics.TOP | Graphics.LEFT);
	}

	private void paintItemNum(Graphics g, int x, int y)
	{

	}

}
